package com.socialcoco.util;

import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsRequest;
import org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsResponse;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.common.text.Text;
import org.elasticsearch.index.query.*;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightField;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@Slf4j
@Component
public class Search {

    @Autowired
    private RestHighLevelClient client;
    public List<Map<String,Object>>SearchName(String keyword) throws IOException {
//        if(pageNO<1){
//            pageNO=1;
//        }
        SearchRequest searchRequest = new SearchRequest("socialuser");
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        MatchQueryBuilder movieName = QueryBuilders.matchQuery("username",keyword);
        searchSourceBuilder.query(movieName);
        HighlightBuilder highlightBuilder = new HighlightBuilder();
        highlightBuilder.field("username");
        highlightBuilder.preTags("<span style='color:red'>");
        highlightBuilder.postTags("</span>");
        searchSourceBuilder.highlighter(highlightBuilder);
        searchRequest.source(searchSourceBuilder);
        SearchResponse search = client.search(searchRequest, RequestOptions.DEFAULT);
        ArrayList<Map<String,Object>> map = new ArrayList<>();
        for (SearchHit hit : search.getHits().getHits()) {
            Map<String, HighlightField> highlightFields = hit.getHighlightFields();
            HighlightField movieName1 = highlightFields.get("username");
            Map<String, Object> sourceAsMap = hit.getSourceAsMap();
            if(movieName1!=null){
                Text[] fragments = movieName1.fragments();
                String n_movie="";
                for (Text text : fragments) {
                    n_movie+=text;
                }
                sourceAsMap.put("username",n_movie);
            }
            map.add(sourceAsMap);
        }
        return map;
    }
    public List<Map<String,Object>>SearchContent(String keyword) throws IOException {
//        if(pageNO<1){
//            pageNO=1;
//        }
        SearchRequest searchRequest = new SearchRequest("diaryrespones");
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
//        searchSourceBuilder.from(pageNO);
//        searchSourceBuilder.size(pageSize);
//        TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("movieName",keyword);
        BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
        MatchQueryBuilder movieName = QueryBuilders.matchQuery("header", keyword);
        boolQueryBuilder.should(movieName);
        boolQueryBuilder.should(QueryBuilders.matchQuery("textarea", keyword));
//        WildcardQueryBuilder movieName1 = QueryBuilders.wildcardQuery("movieName", keyword);
        //高亮
        searchSourceBuilder.query(boolQueryBuilder);
        HighlightBuilder highlightBuilder = new HighlightBuilder();
        highlightBuilder.field("header");
        highlightBuilder.preTags("<span style='color:red'>");
        highlightBuilder.postTags("</span>");
        searchSourceBuilder.highlighter(highlightBuilder);
        searchRequest.source(searchSourceBuilder);
        SearchResponse search = client.search(searchRequest, RequestOptions.DEFAULT);
        ArrayList<Map<String,Object>> map = new ArrayList<>();
        for (SearchHit hit : search.getHits().getHits()) {
            Map<String, HighlightField> highlightFields = hit.getHighlightFields();
            HighlightField movieName1 = highlightFields.get("header");
            Map<String, Object> sourceAsMap = hit.getSourceAsMap();
            if(movieName1!=null){
                Text[] fragments = movieName1.fragments();
                String n_movie="";
                for (Text text : fragments) {
                    n_movie+=text;
                }
                sourceAsMap.put("header",n_movie);
            }
            map.add(sourceAsMap);
        }
        return map;
    }
    //创建索引
    public boolean createIndex(String user) throws IOException {
        CreateIndexRequest createIndexRequest = new CreateIndexRequest(user);
        CreateIndexResponse createIndexResponse = client.indices().create(createIndexRequest, RequestOptions.DEFAULT);
        return createIndexResponse.isAcknowledged();
    }
    //判断索引是否存在
    public boolean indexExists(String index) throws IOException {
        GetIndexRequest request = new GetIndexRequest(index);
        request.indices();
        boolean exists = client.indices().exists(request, RequestOptions.DEFAULT);
        if (exists) {
            return true;
        }
        return false;
    }
}
